/*
 * @Descripttion: 
 * @version: 
 * @Author: sueRimn
 * @Date: 2022-09-30 14:29:50
 * @LastEditors: sueRimn
 * @LastEditTime: 2022-11-29 10:53:39
 */
let util = require("../util");
let operate = require("../db/opeate")
let { query } = require("../db/pool")
exports.add = (req, res) => {
  let params = req.body;
  params.id = util.uuidGenerator();
  let com_code = params.code.replace(new RegExp("[A-Z]", "g"), function (it) {
    return '-' + it.toLowerCase();
  })
  let content = '<' + com_code + ' :originObj="' + params.code + 'Data"    :originElement="' + params.code + 'Element"></' + com_code + '>';
  let strSql = "insert into component(id,code,type,description,sort_number,create_author,last_modified_author,content) values('" + params.id + "' ,'" + params.code + "' ,'" + params.type + "' ,'" + params.description + "' ,'" + params.sort_number + "' ,'" + params.create_author + "' ,'" + params.create_author + "' ,'" + content + "');";
  componentQuery(strSql, res);
}
exports.getList = (req, res) => {
  let params = req.body;
  let wh = 'where ';
  for (let key in params) {
    (!util.isEmpty(params[key])) ? wh += key + "='" + params[key] + "' and " : "";
  }
  wh = wh.substring(0, wh.lastIndexOf("and")) + ";";
  let strSql = "select * from component ";
  wh.trim() != "where" ? strSql += wh : strSql += ";";
  componentQuery(strSql, res);
}
exports.edit = (req, res) => {
  let params = req.body;
  let com_code = params.code.replace(new RegExp("[A-Z]", "g"), function (it) {
    return '-' + it.toLowerCase();
  })
  let content = '<' + com_code + ' :originObj="' + params.code + 'Data"    :originElement="' + params.code + 'Element"></' + com_code + '>';
  let strSql = "update component set code='" + params.code + "' ,description='" + params.description + "' ,sort_number='" + params.sort_number + "' ,last_modified_author='" + params.last_modified_author + "' ,type='" + params.type + "' ,content='" + content + "' where id='" + params.id + "';";
  componentQuery(strSql, res);
}
exports.delete = (req, res) => {
  let params = req.query;
  let strSql = "delete from component where id='" + params.id + "';";
  componentQuery(strSql, res);
}
async function componentQuery(sql, res) {
  let rtn = await query(sql);
  res.send(rtn);
}